package com.wonders.module.auth.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.common.constant.TipConstant;
import com.wonders.framework.util.StringUtil;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.auth.constant.AuthConstant;
import com.wonders.module.auth.entity.vo.RoleAuthVo;
import com.wonders.module.auth.service.RoleAuthService;

/**
 * 角色权限控制层
 * 
 * @author WuBin
 * 
 */
@Controller
@RequestMapping("/auth/roleAuth")
public class RoleAuthController extends BaseController {

	@Autowired
	private RoleAuthService roleAuthService;

	/**
	 * 导航到角色权限编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("toRoleAuth")
	public Object toRoleAuth() {
		return "auth/roleAuth";
	}

	/**
	 * 查询角色包含的权限信息
	 * 
	 * @return
	 */
	@RequestMapping("queryRoleMenu")
	@ResponseBody
	public Object queryRoleMenu() {
		RoleAuthVo roleAuthVo = RequestUtil
				.requestParamToBean(RoleAuthVo.class);
		roleAuthVo.setAuthType(AuthConstant.AuthType.MENU);
		return roleAuthService.queryRoleMenu(roleAuthVo);
	}

	/**
	 * 保存角色权限关系
	 * 
	 * @return
	 */
	@RequestMapping("saveRoleMenu")
	@ResponseBody
	public Object saveRoleMenu() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		Long roleId = Long.valueOf((String) RequestUtil
				.getRequestParam("roleId"));
		if (StringUtils.isEmpty(keys) || roleId == null) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		roleAuthService.saveRoleMenu(roleId,
				StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

}
